package fr.alma.domo.music.impl;

import org.oasisopen.sca.annotation.Destroy;
import org.oasisopen.sca.annotation.Init;
import org.oasisopen.sca.annotation.Reference;
import org.oasisopen.sca.annotation.Service;

import fr.alma.domo.model.AbstractService;
import fr.alma.domo.model.Description;
import fr.alma.domo.music.ClientContext;
import fr.alma.domo.music.ExecutionService;
import fr.alma.domo.music.MusicPlayerSCM;
import fr.alma.domo.music.PositionService;
import fr.alma.domo.music.rc.HP;
import fr.alma.domo.music.rc.Player;
import fr.alma.domo.svc.SelectionAndDiscovery;
import fr.alma.domo.util.Log;

@SuppressWarnings({ "serial", "unused" })
@Service(MusicPlayerSCM.class)
public class MusicPlayerSCMImpl extends AbstractService  implements MusicPlayerSCM {

	public MusicPlayerSCMImpl(){
		super("MusicPlayerSCM");
	}
	
	@Reference(required=true)
	protected PositionService positionService;
	
	@Reference(required=true)
	protected SelectionAndDiscovery selectionAndDiscovery;
	
	@Reference(required=true)
	protected ExecutionService executionService;

	@Override
	public String getPosition(String clientId) {
		return positionService.getPositionById(clientId);
	}

	@Override
	public Player selectPlayer(String title) {
		Description desc = new Description();
		desc.addScope(RESOURCE_FAMILY);
		desc.addProperty(INSTANCE_OF, Player.class);
		desc.addProperty(Player.PLAYING_TITLE, ""); //On suppose que le lecteur ne pas lire 2 titre en meme tps
		desc.addSequenceProperty(Player.SONG_LIST, title);
		return (Player)selectionAndDiscovery.select(desc);
	}

	@Override
	public HP selectHP(String position) {
		Description desc = new Description();
		desc.addScope(RESOURCE_FAMILY);
		desc.addProperty(INSTANCE_OF, HP.class);
		desc.addProperty(HP.POSITION, position);
		return (HP)selectionAndDiscovery.select(desc);
	}

	@Override
	public boolean play(String clientId,Player player, HP hp) {
		ClientContext ctx = positionService.getClientContextById(clientId);
		if(ctx!=null){
			ctx.getHPs().add(hp);
		}
		executionService.execute(player, hp);
		return false;
	}

}
